9/**
 * Created by Administrator on 2016/8/3.
 */
"use strict";
swan_pc
    .controller("ActivityListController",function($scope,$state,$q,getInvitations,InvitationConfirm,InvitationCancel,candidateUserManageById,
                                                  InvitationConflicts,EventManageById,getInvitationById,ClipPost,smallEventManage,BigEventById){
        $scope.init=function() {
            $scope.ActivityList=[];
            $scope.ActivityConfirmed=[];
            $scope.ActivityRefused=[];
            $scope.ActivityPending=[];
            getInvitations.query(function (result) {
                //console.log(result);
                angular.forEach(result,function(data,index,array){
                    if(data.enrollStatus=="PENDING"){
                        $scope.ActivityPending.push(data);
                    }else if(data.enrollStatus=="CONFIRM"){
                        $scope.ActivityConfirmed.push(data);
                    }else if(data.enrollStatus=="REJECT"){
                        $scope.ActivityRefused.push(data);
                    }
                    $scope.ActivityList.push(data);
                });
                //console.log($scope.EventManageState);
                if($scope.EventManageState==1){
                    $scope.showActivity=$scope.ActivityList;
                }else if($scope.EventManageState==3){
                    $scope.showActivity=$scope.ActivityConfirmed;
                }else if($scope.EventManageState==4){
                    $scope.showActivity=$scope.ActivityRefused;
                }else{
                    $scope.showActivity=$scope.ActivityPending;
                }
            })
        };
        $scope.$watch(function(){
            return $scope.$parent.EventManageState;
        },function(){
            $scope.init();
        });
        //活动类型展示
        $scope.activityType_show=function(option){
            var type="";
            if(option=="INDIVIDUALEVENT"){
                type="个人活动邀请";
            }else if(option=="SMALLEVENT"){
                type="组织小型活动";
            }else if(option=="EVENT"){
                type="组织大型活动";
            }
            return type;
        };
        //approveStatus展示
        $scope.approveStatus_show=function(option){
            var name="";
            if(option=="PENDING"){
                name="待定";
            }else if(option=="WAITING"){
                name="排队等待";
            }else if(option=="CONFIRM"){
                name="已批准";
            }else if(option=="REJECT"){
                name="未通过";
            }else if(option=="CANCEL"){
                name="取消";
            }
            return name;
        };
        //enroll状态展示
        $scope.userEnrollStatus_show=function(option){
            //console.log(option);
            var enrollStatusName="";
            if(option=="PENDING"){
                enrollStatusName="待定";
            }else if(option=="CONFIRM"){
                enrollStatusName="确认参加";
            }else if(option=="REJECT"){
                enrollStatusName="拒绝参加";
            }else if(option=="CANCEL"){
                enrollStatusName="取消";
            }
            return enrollStatusName;
        }
        //跳转页面至邀请详细列表
        $scope.changeStateToActivityDetail=function(id){
            $scope.$parent.activityDetailId=id;
            $state.go("activitydetail")
        }
        //接受活动邀请
        $scope.conflictClips=[];
        $scope.newClip={
            "actionRemark": null,
            "address": null,
            "advanceInfo": null,
            "category": null,
            "checkList": null,
            "code": null,
            "color": null,
            "deletedAt": null,
            "endTime": null,
            "individualTaskId": null,
            "isDelayed": null,
            "isFixed": null,
            "isImportant": null,
            "isPrincipal": null,
            "isSecret": null,
            "level": null,
            "linkedActivityId": null,
            "linkedActivityType": null,
            "name": null,
            "organizationId": null,
            "ownerId": null,
            "previewTime": null,
            "relatedCompany": null,
            "relatedContact":null,
            "relatedOpp": null,
            "remark": null,
            "remind": null,
            "startTime":null,
            "status":null,
            "type": null,
            "who": null
        }
        $scope.confirmToActivity=function(id){
            var def=$q.defer();
            var pro=def.promise;
            InvitationConflicts.query({id:id},function(result){
                $scope.conflictClips=result;
                $scope.presentActivity={};
                $scope.presentEventDetail={};
                console.log(id);
                getInvitationById.get({id:id},function(result) {
                    $scope.presentActivity = result;
                    console.log($scope.presentActivity);
                    if($scope.presentActivity.activityType=="INDIVIDUALEVENT") {
                        EventManageById.get({id: $scope.presentActivity.activityId}, function (result) {
                            $scope.presentEventDetail = result;
                            def.resolve();
                        })
                    }else if($scope.presentActivity.activityType=="SMALLEVENT"){
                        smallEventManage.get({id: $scope.presentActivity.activityId}, function (result) {
                            $scope.presentEventDetail = result;
                            def.resolve();
                        })
                    }else if($scope.presentActivity.activityType=="EVENT"){
                        BigEventById.get({id: $scope.presentActivity.activityId}, function (result) {
                            $scope.presentEventDetail = result;
                            def.resolve();
                        })
                    }
                })
                pro.then(function(){
                     /*if($scope.conflictClips.length>0 && $scope.presentEventDetail.activityType!="EVENT"){
                        swal("您当前的clip与此项邀请冲突","请处理后再接受此邀请","error");
                    }else{*/
                        InvitationConfirm.update(
                            {id:id},function() {
                                $scope.init();
                                $scope.newClip.name = $scope.presentEventDetail.name;
                                $scope.newClip.startTime = $scope.presentEventDetail.startTime;
                                $scope.newClip.endTime = $scope.presentEventDetail.endTime;
                                $scope.newClip.address = $scope.presentEventDetail.address;
                                $scope.newClip.status = "CONFIRM";
                                $scope.newClip.remark = $scope.presentEventDetail.remark;
                                $scope.newClip.level = "主事项";
                                $scope.newClip.type = "会议";
                                swal("您已接受好友活动邀请", "", "success");
                               if ($scope.presentEventDetail.activityType != "EVENT") {
                                    ClipPost.save($scope.newClip, function (result) {
                                        console.log(result);
                                    }, function (result) {
                                        console.log(result);
                                    })
                                }
                            },
                            function(result){
                                console.log(result);
                                swal("接受好友邀请失败",result.data.description,"error");
                            }
                        )
                   // }
                })
            })
        }
        //拒绝活动邀请
        $scope.cancelToActivity=function(id){
            InvitationCancel.update(
                {id:id},function(){
                    $scope.init();
                    swal("您已拒绝好友活动邀请","","success");
                },function(result){
                    console.log(result);
                    swal("拒绝好友邀请失败","","error");
                }
            )
        }
        //删除该活动邀请
        $scope.deleteActivity=function(id){
            candidateUserManageById.delete(
                {id:id},function(){
                    $scope.init();
                    swal("删除好友活动邀请成功","","success");
                },
                function(result){
                    swal("删除好友活动邀请失败","","error");
                }
            )
        }
    })